package com.rookie.jeesdp.controller;

import com.rookie.jeesdp.entity.admin.LoginUser;
import com.rookie.jeesdp.entity.common.Result;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

@RestController
public class UserController {

    @RequestMapping("login")
    public ModelAndView login() {
        return new ModelAndView("login");
    }

    @RequestMapping("user")
    public LoginUser user() {
        OAuth2Authentication authentication = (OAuth2Authentication) SecurityContextHolder.getContext().getAuthentication();
        return (LoginUser) authentication.getUserAuthentication().getPrincipal();
    }

    @RequestMapping("logout")
    public Result logout() {
        SecurityContextHolder.clearContext();
        return Result.success();
    }

}
